package com.coollang.smartbra.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.andexert.library.RippleView;
import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.ActionDetailsAdapter1;
import com.coollang.smartbra.beans.ActionData;
import com.coollang.smartbra.fragment.MyDialogFragment;
import com.coollang.smartbra.utils.TimeUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActionDetailsActivity extends MyAutoLayoutActivity implements RippleView.OnRippleCompleteListener {

    @BindView(R.id.status_bar)
    View mStatusBar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.rv_back)
    RippleView rvBack;
    @BindView(R.id.listview)
    ListView mListView;
    @BindView(R.id.tv_second)
    TextView mTvSecond;
    @BindView(R.id.recycleview)
    RecyclerView mRecyclerView;

    private ArrayList<Integer> mList;
    private ArrayList<ActionData> mActionDataList;
    private ActionDetailsAdapter1 mAdapter1;
    private ActionData mClickActionData;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_action_details);
        ButterKnife.bind(this);

        // 设置沉浸式状态栏
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(mStatusBar, this, content);

        initViews();
        initListener();

        Intent intent = getIntent();
        if (intent != null) {
            mList = getIntent().getIntegerArrayListExtra("list");
            mActionDataList = getIntent().getParcelableArrayListExtra("actionlist");

            Collections.reverse(mList);
            Collections.reverse(mActionDataList);

            mAdapter1.setList(mList, mActionDataList);
            if (mList != null && mList.size() > 0) {
                mTvSecond.setText(TimeUtils.getFormatmillisecond(mList.get(mList.size() - 1)));
            }
        }
    }

    private void initViews() {
        tvTitle.setText("动作详情");
        tvTitle.setTextColor(getResources().getColor(R.color.black_80));

        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/enfragment_type.ttf");
        //设置字体
        mTvSecond.setTypeface(typeFace);

        mAdapter1 = new ActionDetailsAdapter1(this);
        mRecyclerView.setAdapter(mAdapter1);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    }

    private void initListener() {
        rvBack.setOnRippleCompleteListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onComplete(RippleView rippleView) {
        finish();
    }


    public void onClickErrorItem(int position) {
        mClickActionData = mAdapter1.getItem(position);
        showDialog();
    }

    private void showDialog() {
        MyDialogFragment dialogFragment = new MyDialogFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        dialogFragment.show(fragmentManager, "MyDialogFragment");

        dialogFragment.setOnDialogListener(new MyDialogFragment.OnDialogListener() {
            @Override
            public int onClickNext() {
                // 点击了下一首
                int position = mAdapter1.getItemPosition(mClickActionData);

                if (position == mAdapter1.getItemCount() - 1) {
                    return 0;
                } else {
                    // 获取上一条错误视频
                    mClickActionData = mAdapter1.getItem(++position);
                    return 1;
                }
            }

            @Override
            public String getVideoPath() {
                return getPlayVideoPath();
            }

            @Override
            public int onClickLast() {
                int position = mAdapter1.getItemPosition(mClickActionData);

                if (position == 0) {
                    return 0;
                } else {
                    // 获取下一条错误视频
                    mClickActionData = mAdapter1.getItem(--position);
                    return 1;
                }
            }
        });
    }

    public String getPlayVideoPath() {

        return "android.resource://" + getPackageName() + "/" + mClickActionData.getErrorVideo();
    }
}
